package com.shiguiwu.springmybatis.designpattern.prototype.scene.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @description: 打乱题目顺序
 * @author: stone
 * @date: Created by 2021/8/18 0:00
 * @version: 1.0.0
 * @pakeage: com.shiguiwu.springmybatis.designpattern.prototype.scene.util
 */
public class TopicRandomUtil {


    /**
     * 乱序Map元素，记录对应答案key
     * @param option 题⽬目
     * @param key 答案
     * @return Topic 乱序后 {A=c., B=d., C=a., D=b.}
     */
     static public Topic random(Map<String, String> option, String key) {
         Set<String> keySet = option.keySet();

         List<String> optionList = new ArrayList<>(keySet);
         Collections.shuffle(optionList);
         Map<String, String> newOption = new HashMap<>();
         String newKey = "";
         int t = 0;
         for (String next : keySet) {
             String randomKey = optionList.get(t++);
             if (key.equals(next)) {
                 newKey = randomKey;
             }
             newOption.put(randomKey, option.get(next));
         }
         return new Topic(newKey, newOption);
    }
}
